package com.young.filesystem.utils;

import java.io.File;

public class FileSizeCalculator {

  private static int getFolderFileCount(File folder) {
    if (folder == null || !folder.exists()) return -1;
    int count = 0;
    File[] files = folder.listFiles();
    for (File file : files) {
      if (file.isDirectory()) {
        count += getFolderFileCount(file);
      } else {
        count++;
      }
    }
    return count;
  }

  private static long getTotalFolderSize(File folder) {
    if (folder == null || !folder.exists()) return -1L;
    long size = 0;
    File[] files = folder.listFiles();
    for (File file : files) {
      if (file.isDirectory()) {
        size += getTotalFolderSize(file);
      } else {
        size += file.length();
      }
    }

    return size;
  }

  public static void main(String[] args) {
    String folderPath = "D:\\profiles"; // 指定文件夹路径

    long fileCount = getFolderFileCount(new File(folderPath));
    long totalSize = getTotalFolderSize(new File(folderPath));

    System.out.println("文件数量：" + fileCount);
    System.out.println("总大小：" + totalSize + " bytes");
  }
}

